package com.sprite.htr.spritehtr.view;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.sprite.htr.spritehtr.R;
import com.sprite.htr.spritehtr.util.AnimUtil;

/**
 * Created by admin on 2017/7/1.
 */

public class ShopView extends BaseView{
    private ImageView iv_chongzhi;
    private ImageView iv_fugure;
    private AnimationSet animSetFugure;
    private FrameLayout fl_btns;
    private ChongZhiView chongZhiView;


    public ShopView(Context context){
        super(context);
        setContentView(R.layout.view_shop);

        initUI();
        fugureAnimation();
    }

    private void fugureAnimation(){
        animSetFugure = new AnimationSet(false);
        ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 1f, 0.9f, 0.8f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1f);
        scaleAnimation.setDuration(800);
        scaleAnimation.setRepeatCount(Animation.INFINITE);//无限重复
        scaleAnimation.setRepeatMode(Animation.REVERSE);
        animSetFugure.addAnimation(scaleAnimation);


        TranslateAnimation tranX = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
        tranX.setDuration(300);
        animSetFugure.addAnimation(tranX);
    }

    public void show(){
        TranslateAnimation tranY = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 0f);
        tranY.setDuration(300);
        fl_btns.startAnimation(tranY);
        tranY.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iv_fugure.setVisibility(View.VISIBLE);
                iv_fugure.startAnimation(animSetFugure);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    public void hide(Animation.AnimationListener listener){
        TranslateAnimation tranY = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1f);
        tranY.setDuration(300);
        fl_btns.startAnimation(tranY);

        TranslateAnimation tranX = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
        tranX.setDuration(300);
        iv_fugure.startAnimation(tranX);
        if (listener != null){
            tranX.setAnimationListener(listener);
        }
    }

    @Override
    protected void initUI(){
        iv_chongzhi = (ImageView) findViewById(R.id.iv_chongzhi);
        AnimUtil.addOnTouchListener(iv_chongzhi, 0.5f, 0.5f, new AnimUtil.AnimListener() {
            @Override
            public void down(View v) {

            }

            @Override
            public void move(View v) {

            }

            @Override
            public void up(View v) {
                chongZhiView.show();
            }
        });
        iv_fugure = (ImageView) findViewById(R.id.iv_figure);
        iv_fugure.setVisibility(View.INVISIBLE);
        fl_btns = (FrameLayout) findViewById(R.id.fl_btns);

        chongZhiView = new ChongZhiView(context);
    }
}
